<template>
	<div class="login">
		<div class="login_box">
			<el-row :gutter="24" class="row">
				<el-col :xs="24" :sm="24" :lg="12" class="hidden-md-and-down">
					<img src="@/assets/images/login_left_box.png" class="left_img" />
				</el-col>
				<el-col :xs="24" :sm="24" :lg="12">
					<el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" v-if="show">
						<h2 class="title">登录</h2>
						<el-form-item prop="username" style="margin-top: 13px;">
							<el-input style="font-size: 28px;" v-model="loginForm.username" type="text"
								auto-complete="off" placeholder="账号">
							</el-input>
						</el-form-item>
						<el-form-item prop="password">
							<el-input v-model="loginForm.password" type="password" auto-complete="off" show-password
								placeholder="密码" @keyup.enter.native="handleLogin">
								<svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
							</el-input>
						</el-form-item>
						<el-form-item prop="code">
							<el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 60%"
								@keyup.enter.native="handleLogin">
							</el-input>
							<div class="login-code">
								<img :src="codeUrl" @click="getCode" class="login-code-img" />
							</div>
						</el-form-item>
						<el-form-item style="width:100%;">
							<el-button :loading="loading" size="medium" type="primary" class="submit_btn"
								@click.native.prevent="handleLogin">
								<span v-if="!loading" style="font-size: 18px;">登 录</span>
								<span v-else style="font-size: 18px;">登 录 中...</span>
							</el-button>
						</el-form-item>
						<el-row :gutter="24">
							<el-col :span="12" style="text-align: left;">
								<el-checkbox v-model="loginForm.rememberMe"
									style="margin:0px 0px 25px 0px;font-size: 14px;">记住密码</el-checkbox>
							</el-col>
							<el-col :span="12" style="text-align: right;">
								<el-link style="color: #3672fe;font-size: 14px;" @click='btn_password'>忘记密码</el-link>
							</el-col>
						</el-row>
					</el-form>

					<!-- 找回密码-->
					<el-form ref="loginForm" class="login-form" v-if="show2">
						<h2 class="title">找回密码</h2>
						<el-form-item prop="name" style="margin-top: 13px;">
							<el-input style="font-size: 28px;" v-model="name" type="text" auto-complete="off"
								placeholder="请输入姓名" @input='ipt1'>
							</el-input>
						</el-form-item>
						<el-form-item prop="card" style="margin-top: 13px;">
							<el-input style="font-size: 28px;" v-model="card" type="text" auto-complete="off"
								placeholder="请输入身份证号" @input='ipt2'>
							</el-input>
							
						</el-form-item>
						<el-form-item prop="card" style="margin-top: 13px;">
							<el-input style="font-size: 28px;" v-model="studentId" type="text" auto-complete="off"
								placeholder="请输入学工号" @input='ipt4'>
							</el-input>
							<el-button :loading="loading" size="medium" type="primary" class="submit_btn"
								@click.native.prevent="handleLogin2" v-if="names&&cards&&studentIds">
								<span style="font-size: 18px;">下一步</span>
							</el-button>
							<el-button size="medium" type="info" class="submit_btn2"
								@click.native.prevent="handleLogin2" v-if="!names||!cards||!studentIds">
								<span style="font-size: 18px;">下一步</span>
							</el-button>
						</el-form-item>
						<el-row :gutter="24">
							<el-col :span="12" style="text-align: left; color: #1890ff;font-size: 14px;cursor: pointer;">
								<span @click='btn()'>立即登录</span>
								
							</el-col>
							<el-col :span="12" style="text-align: right;">
					
							</el-col>
						</el-row>
					</el-form>
					<!-- 新密码-->
					<el-form ref="loginForm" class="login-form" v-if="show3">
						<h2 class="title">找回密码</h2>
						<el-form-item prop="x_password" style="margin-top: 13px;">
							<el-input style="font-size: 28px;" v-model="x_password" type="text" auto-complete="off"
								placeholder="请输入8-18位新密码" @input='ipt3'>
							</el-input>
						</el-form-item>
						<el-button :loading="loading" size="medium" type="primary" class="submit_btn"
							@click.native.prevent="handleLogin3" v-if="x_passwords">
							<span style="font-size: 18px;">完成</span>
						</el-button>
						<el-button size="medium" type="info" class="submit_btn2" @click.native.prevent="handleLogin3"
							v-if="!x_passwords">
							<span style="font-size: 18px;">完成</span>
						</el-button>
						</el-form-item>
						<el-row :gutter="24">
							<el-col :span="12" style="text-align: left; color: #1890ff;font-size: 14px;cursor: pointer; margin-top: 20px;" >
								
								<span @click='btn()'>立即登录</span>
								
							</el-col>
							<el-col :span="12" style="text-align: right;">
											
							</el-col>
						</el-row>
					</el-form>
				</el-col>
			</el-row>
		</div>
		<!--  底部  -->
		<div class="el-login-footer">
			<span>Copyright © 2021 南昌工学院</span>
		</div>
	</div>
</template>

<script>
	import {
		getCodeImg,
		retrievePassword
	} from '@/api/login'
	import Cookies from 'js-cookie'
	import {
		encrypt,
		decrypt
	} from '@/utils/jsencrypt'
	import 'element-ui/lib/theme-chalk/display.css'

	export default {
		name: 'Login',
		components: {},
		data() {
			return {
				codeUrl: '',
				cookiePassword: '',
				loginForm: {
					username: 'admin',
					password: '123456',
					rememberMe: false,
					code: '',
					uuid: ''
				},
				loginRules: {
					username: [{
						required: true,
						trigger: 'blur',
						message: '用户名不能为空'
					}],
					password: [{
						required: true,
						trigger: 'blur',
						message: '密码不能为空'
					}],
					code: [{
						required: true,
						trigger: 'change',
						message: '验证码不能为空'
					}]
				},
				loading: false,
				redirect: undefined,
				show: true,
				show2: false,
				show3: false,
				name: "",
				card: "",
				names: false,
				cards: false,
				x_passwords:false,
				x_password: '',
				studentId:"",//学工号
				studentIds:false,

			}
		},
		watch: {
			$route: {
				handler: function(route) {
					this.redirect = route.query && route.query.redirect
				},
				immediate: true
			}
		},
		created() {
			this.getCode()
			this.getCookie()
		},
		methods: {
			/* 找回密码 */
			retrieve_btn(){
				let obj={
					card:this.card,
					nickName:this.name,
					password:this.x_password,
					studentId:this.studentId
				}
				retrievePassword(obj).then(res=>{
					if(res.code==200){
						this.$message({
						          message: res.msg,
						          type: 'success'
						        });
								this.show = true
								this.show2 = false
								this.show3=false
					}else{
						  this.$message.error(res.msg);
					}
				})
			},
			btn(){
				this.show=true
				this.show2=false
				this.show3=false
			},
			btn_password() {
				this.show = false
				this.show2 = true
				this.show3=false
			},
			handleLogin2() {
				if (!this.names || !this.cards) {
					this.$message({
						message: '请输入正确的信息',
						type: 'warning'
					});
					
				}else{
					this.show = false
					this.show2 = false
					this.show3=true
				}
				
			},
			handleLogin3() {
				if (!this.x_passwords) {
					this.$message({
						message: '密码不得少于8位',
						type: 'warning'
					});
					return false
				}
				this.retrieve_btn()
			},
			ipt1() {
				if (this.name != '') {
					this.names = true
				} else {
					this.names = false
				}
			},
			ipt2() {
				if (!(/^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(
						this.card))) {
					this.cards = false
				} else {
					this.cards = true
				}
			},
			ipt3() {
				if (this.x_password.length>=8) {
					this.x_passwords=true
				} else {
					this.x_passwords=false
				}
			},
			ipt4(){
				if (this.studentId!='') {
					this.studentIds=true
				} else {
					this.studentIds=false
				}
			},
			getCode() {
				getCodeImg().then(res => {
					this.codeUrl = 'data:image/gif;base64,' + res.img
					this.loginForm.uuid = res.uuid
				})
			},
			getCookie() {
				const username = Cookies.get('username')
				const password = Cookies.get('password')
				const rememberMe = Cookies.get('rememberMe')
				this.loginForm = {
					username: username === undefined ? this.loginForm.username : username,
					password: password === undefined ? this.loginForm.password : decrypt(password),
					rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
				}
			},
			handleLogin() {
				this.$refs.loginForm.validate(valid => {
					if (valid) {
						this.loading = true
						if (this.loginForm.rememberMe) {
							Cookies.set('username', this.loginForm.username, {
								expires: 30
							})
							Cookies.set('password', encrypt(this.loginForm.password), {
								expires: 30
							})
							Cookies.set('rememberMe', this.loginForm.rememberMe, {
								expires: 30
							})
						} else {
							Cookies.remove('username')
							Cookies.remove('password')
							Cookies.remove('rememberMe')
						}
						this.$store
							.dispatch('Login', this.loginForm)
							.then(() => {
								this.$router.push({
									path: this.redirect || '/'
								}).catch(() => {})
							})
							.catch(() => {
								this.loading = false
								this.getCode()
							})
					}
				})
			}
		}
	}
</script>

<style rel="stylesheet/scss" lang="scss">
	.login_box {
		width: 1533px;
		height: 717px;
		background: #fff;
		box-shadow: 0px 0px 15px 0px rgba(0, 0, 0, 0.05);
		overflow: hidden;
		border-radius: 10px;

		.left_img {
			background-size: 100% 100%;
		}

		.row {
			height: 100%;
		}
	}

	@media only screen and (max-width: 992px) {
		.login {
			display: block !important;
			padding-top: 100px;
		}

		.login_box {
			height: auto;
			margin: 0 auto;
			width: 95%;
		}

		.login-form {
			box-sizing: border-box !important;
			padding: 10px 10px 0px 10px !important;
		}
	}

	.login {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 100%;
		min-height: 800px;
		background-image: url('../../assets/images/login-background.jpg');
		background-size: cover;
		position: relative;
		left: 0px;
		top: 0px;
	}

	.title {
		margin: 0px auto 30px auto;
		text-align: center;
		color: #707070;
	}

	.logo_line {
		width: 100%;
		box-sizing: border-box;
		padding-bottom: 20px;
		display: flex;
		align-items: center;
		justify-content: center;

		.img {
			width: 169px;
			height: 174px;
		}
	}

	.login-form {
		width: 100%;
		box-sizing: border-box;
		padding: 88px 88px 0px 102px;

		.title {
			line-height: 46px;
			font-size: 35px;
			color: #333;
			text-align: center;
		}

		.el-input {
			height: 89px;

			input {
				padding: 0px 27px;
				font-size: 22px;
				height: 89px;
			}

			.el-input__suffix>.el-input__suffix-inner>i {
				font-size: 22px;
			}
		}

		.input-icon {
			height: 39px;
			width: 14px;
			margin-left: 2px;
		}
	}

	.login-tip {
		font-size: 13px;
		text-align: center;
		color: #bfbfbf;
	}

	.login-code {
		width: 38%;
		height: 89px;
		float: right;

		img {
			width: 100%;
			height: 100%;
			cursor: pointer;
			vertical-align: middle;
		}
	}

	.el-login-footer {
		height: 40px;
		line-height: 40px;
		position: absolute;
		bottom: 0;
		width: 100%;
		text-align: center;
		color: #999;
		font-family: Arial;
		font-size: 14px;
		letter-spacing: 1px;
	}

	.login-code-img {
		height: 38px;
	}

	.login-enter-active {
		animation: fadeInDownBig 1s;
	}

	@-webkit-keyframes fadeInDownBig {
		from {
			opacity: 0;
			-webkit-transform: translate3d(0, -2000px, 0);
			transform: translate3d(0, -2000px, 0);
		}

		to {
			opacity: 1;
			-webkit-transform: translate3d(0, 0, 0);
			transform: translate3d(0, 0, 0);
		}
	}

	@keyframes fadeInDownBig {
		from {
			opacity: 0;
			-webkit-transform: translate3d(0, -2000px, 0);
			transform: translate3d(0, -2000px, 0);
		}

		to {
			opacity: 1;
			-webkit-transform: translate3d(0, 0, 0);
			transform: translate3d(0, 0, 0);
		}
	}

	.submit_btn {
		background: linear-gradient(#428dff, #3672fe);
		box-shadow: 0px 0px 20px 0px rgba(52, 115, 254, 0.73);
		margin-top: 46px;
		width: 100%;
		font-size: 18px !important;
		font-weight: bold;
		height: 89px;

		&:hover {
			background: linear-gradient(#428dff, #3672fe);
			opacity: 0.8;
		}
	}

	.submit_btn2 {
		margin-top: 46px;
		width: 100%;
		font-size: 18px !important;
		font-weight: bold;
		height: 89px;
		background: #999;

		&:hover {
			background: linear-gradient(#999, #999);
			opacity: 0.8;
		}
	}
</style>
